﻿using System;
using System.Drawing;
using System.Windows.Forms;
using HDM.NexusEngine.Core;
using HDM.NexusEngine.Graphics;
using HDM.NexusEngine.Input;
using Tao.DevIl;
using Tao.OpenGl;

namespace HDM.NexusEngine.EngineTest
{
    public partial class Form1 : Form
    {
        private bool _fullScreen = false;
        private FastLoop _fastLoop;
        private readonly StateSystem _system = new StateSystem();
        private readonly InputManager _input = new InputManager();
        private readonly TextureManager _textureManager = new TextureManager();
        private readonly SoundManager _soundManager = new SoundManager();

        public Form1()
        {
            InitializeComponent();
            var graphicsDevice = new GraphicsDeviceManager(this);
            var renderManager = new RenderManager(graphicsDevice);
            renderManager.Init();
            //simpleOpenGlControl1.InitializeContexts();
            //_input.Mouse = new Mouse(this, simpleOpenGlControl1);
            //_input.Keyboard = new Keyboard(simpleOpenGlControl1);

            //InitializeDisplay();
            //InitializeSounds();
            //InitializeTextures();
            //InitializeGameState();

            //_fastLoop = new FastLoop(GameLoop);
        }

        

        private void InitializeDisplay()
        {
            if (_fullScreen)
            {
                FormBorderStyle = FormBorderStyle.None;
                WindowState = FormWindowState.Maximized;
            }
            else
            {
                ClientSize = new Size(1280, 720);
            }

            Setup2DGraphics(ClientSize.Width, ClientSize.Height);
            //Setup3DGraphics(ClientSize.Width, ClientSize.Height);
        }

        private void InitializeSounds()
        {
            _soundManager.LoadSound("effect", @"Assets/soundeffect1.wav");
            _soundManager.LoadSound("effect2", @"Assets/soundeffect2.wav");
        }

        private void InitializeTextures()
        {
            // Init DevIl
            Il.ilInit();
            Ilu.iluInit();
            Ilut.ilutInit();
            Ilut.ilutRenderer(Ilut.ILUT_OPENGL);

            // Load textures here using the texture manager.
            _textureManager.LoadTexture("spaceship1", "Assets/spaceship.tga");
            _textureManager.LoadTexture("spaceship2", "Assets/spaceship2.tga");
            _textureManager.LoadTexture("start", "Assets/start.tga");
            _textureManager.LoadTexture("finish", "Assets/finish.tga");
            _textureManager.LoadTexture("sky", "Assets/sky.tga");
            _textureManager.LoadTexture("wall", "Assets/wall.tga");
        }

        private void InitializeGameState()
        {
            // Load the game states here.
            _system.AddState("texture_state", new MultipleTexturesState(_textureManager));
            _system.AddState("sound_test", new SoundTestState(_soundManager));
            _system.AddState("input_test", new InputTestState());
            _system.AddState("mouse_test", new MouseTestState(_input));
            _system.AddState("3D_test", new Test3DState());
            _system.AddState("tile_map_state", new TileMapState(_textureManager));
            _system.AddState("platform_state" , new PlatfomerTestState(_input));

            _system.ChangeState("platform_state");
        }

        private void UpdateInput(double elapsedTime)
        {
            _input.Update(elapsedTime);
        }

        private void GameLoop(double elapsedTime)
        {
            UpdateInput(elapsedTime);
            _system.Update(elapsedTime);
            _system.Render();
            //simpleOpenGlControl1.Refresh();
        }

        private void Setup2DGraphics(double width, double height)
        {
            var halfWidth = width/2;
            var halfHeight = height/2;
            Gl.glMatrixMode(Gl.GL_PROJECTION);
            Gl.glLoadIdentity();
            Gl.glOrtho(-halfWidth, halfWidth, -halfHeight, halfHeight, -100, 100);
            Gl.glMatrixMode(Gl.GL_MODELVIEW);
            Gl.glLoadIdentity();
        }

        private void Setup3DGraphics(double width, double height)
        {
            var halfWidth = width / 2;
            var halfHeight = height / 2;
            Gl.glMatrixMode(Gl.GL_PROJECTION);
            Gl.glLoadIdentity();
            Glu.gluPerspective(90, 4/3, 1, 1000);
            Gl.glMatrixMode(Gl.GL_MODELVIEW);
            Gl.glLoadIdentity();
        }

        protected override void OnClientSizeChanged(EventArgs e)
        {
            base.OnClientSizeChanged(e);
            //Gl.glViewport(0, 0, ClientSize.Width, ClientSize.Height);
            //Setup2DGraphics(ClientSize.Width, ClientSize.Height);
            //Setup3DGraphics(ClientSize.Width, ClientSize.Height);
        }
    }
}
